<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <a-table :dataSource="tickets"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'seatCol'">
        <span v-for="item in SEAT_COL_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatCol">
            {{item.desc}}
          </span>
        </span>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <a-modal v-model:visible="visible" title="车票" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="ticket" :label-col="{span: 4}" :wrapper-col="{ span: 20 }">
      <a-form-item label="会员id">
        <a-input v-model:value="ticket.memberId" />
      </a-form-item>
      <a-form-item label="乘客id">
        <a-input v-model:value="ticket.passengerId" />
      </a-form-item>
      <a-form-item label="乘客姓名">
        <a-input v-model:value="ticket.passengerName" />
      </a-form-item>
      <a-form-item label="日期">
        <a-date-picker v-model:value="ticket.trainDate" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
      </a-form-item>
      <a-form-item label="车次编号">
        <a-input v-model:value="ticket.trainCode" />
      </a-form-item>
      <a-form-item label="箱序">
        <a-input v-model:value="ticket.carriageIndex" />
      </a-form-item>
      <a-form-item label="排号">
        <a-input v-model:value="ticket.seatRow" />
      </a-form-item>
      <a-form-item label="列号">
        <a-select v-model:value="ticket.seatCol">
          <a-select-option v-for="item in SEAT_COL_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="出发站">
        <a-input v-model:value="ticket.startStation" />
      </a-form-item>
      <a-form-item label="出发时间">
        <a-time-picker v-model:value="ticket.startTime" valueFormat="HH:mm:ss" placeholder="请选择时间" />
      </a-form-item>
      <a-form-item label="到达站">
        <a-input v-model:value="ticket.endStation" />
      </a-form-item>
      <a-form-item label="到站时间">
        <a-time-picker v-model:value="ticket.endTime" valueFormat="HH:mm:ss" placeholder="请选择时间" />
      </a-form-item>
      <a-form-item label="座位类型">
        <a-select v-model:value="ticket.seatType">
          <a-select-option v-for="item in SEAT_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import {ref, onMounted } from 'vue';
import {notification} from "ant-design-vue";
import axios from "axios";
const SEAT_COL_ARRAY = window.SEAT_COL_ARRAY;
const SEAT_TYPE_ARRAY = window.SEAT_TYPE_ARRAY;
const visible = ref(false);
let ticket = ref({
  id: undefined,
  memberId: undefined,
  passengerId: undefined,
  passengerName: undefined,
  trainDate: undefined,
  trainCode: undefined,
  carriageIndex: undefined,
  seatRow: undefined,
  seatCol: undefined,
  startStation: undefined,
  startTime: undefined,
  endStation: undefined,
  endTime: undefined,
  seatType: undefined,
  createTime: undefined,
  updateTime: undefined,
});
const tickets = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 10,
});
let loading = ref(false);
const columns = [
  {
    title: '会员id',
    dataIndex: 'memberId',
    key: 'memberId',
  },
  {
    title: '乘客id',
    dataIndex: 'passengerId',
    key: 'passengerId',
  },
  {
    title: '乘客姓名',
    dataIndex: 'passengerName',
    key: 'passengerName',
  },
  {
    title: '日期',
    dataIndex: 'trainDate',
    key: 'trainDate',
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '箱序',
    dataIndex: 'carriageIndex',
    key: 'carriageIndex',
  },
  {
    title: '排号',
    dataIndex: 'seatRow',
    key: 'seatRow',
  },
  {
    title: '列号',
    dataIndex: 'seatCol',
    key: 'seatCol',
  },
  {
    title: '出发站',
    dataIndex: 'startStation',
    key: 'startStation',
  },
  {
    title: '出发时间',
    dataIndex: 'startTime',
    key: 'startTime',
  },
  {
    title: '到达站',
    dataIndex: 'endStation',
    key: 'endStation',
  },
  {
    title: '到站时间',
    dataIndex: 'endTime',
    key: 'endTime',
  },
  {
    title: '座位类型',
    dataIndex: 'seatType',
    key: 'seatType',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];

const onAdd = () => {
  ticket.value = {};
  visible.value = true;
};

const onEdit = (record) => {
  ticket.value = window.Tool.copy(record);
  visible.value = true;
};

const onDelete = (record) => {
  axios.delete("/member/admin/ticket/delete/" + record.id).then((response) => {
    const data = response.data;
    if (data.success) {
      notification.success({description: "删除成功！"});
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      notification.error({description: data.message});
    }
  });
};

const handleOk = () => {
  axios.post("/member/admin/ticket/save", ticket.value).then((response) => {
    let data = response.data;
    if (data.success) {
      notification.success({description: "保存成功！"});
      visible.value = false;
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize
      });
    } else {
      notification.error({description: data.message});
    }
  });
};

const handleQuery = (param) => {
  if (!param) {
    param = {
      page: 1,
      size: pagination.value.pageSize
    };
  }
  loading.value = true;
  axios.get("/member/admin/ticket/query-list", {
    params: {
      page: param.page,
      size: param.size
    }
  }).then((response) => {
    loading.value = false;
    let data = response.data;
    if (data.success) {
      tickets.value = data.content.list;
      // 设置分页控件的值
      pagination.value.current = param.page;
      pagination.value.total = data.content.total;
    } else {
      notification.error({description: data.message});
    }
  });
};

const handleTableChange = (page) => {
  // console.log("看看自带的分页参数都有啥：" + JSON.stringify(page));
  pagination.value.pageSize = page.pageSize;
  handleQuery({
    page: page.current,
    size: page.pageSize
  });
};

onMounted(() => {
  handleQuery({
    page: 1,
    size: pagination.value.pageSize
  });
});
</script>
